//
//  RemindMeBatchView.swift
//  ANOA
//
//  Created by hudan on 2025/6/4.
//  Copyright © 2025 aowang. All rights reserved.
//

import UIKit

/// 批量操作类型
enum RemindMeBatchType {
    /// 评论
    case comment
    /// 已阅
    case readed
}

final class RemindMeBatchView: UIView {
    
    static func make(type: RemindMeBatchType, complete: ((RemindMeBatchType, String?) -> Void)?) -> RemindMeBatchView {
        let safeArea = UIApplication.shared.windows.first?.safeAreaInsets.bottom ?? 0.0
        var height = 10 + 20 + 44 + safeArea
        if type == .comment {
            height += 20 + 80
        }
        let view = RemindMeBatchView(frame: CGRect(origin: .zero, size: CGSize(width: KS_SCREEN_WIDTH, height: height)))
        view.type = type
        view.commitBlock = complete
        return view
    }
    
    /// 操作回调: (批量操作类型, 评论内容)
    private var commitBlock: ((RemindMeBatchType, String?) -> Void)?
    
    private var type: RemindMeBatchType = .readed {
        didSet {
            inputTextView.extHidden(hidden: type == .readed)
            commitBtn.extTitle(title: type == .readed ? "批量已阅" : "批量评论")
        }
    }
    
    private lazy var inputTextView: UITextView_P = {
        let txtView = UITextView_P().extHidden(hidden: true)
            .extBgColor(color: Theme.BACKGROUND_COLOR)
            .extRadius(radius: 5).extMaskBounds(mask: true)
        txtView.placeHolder = "请输入评论内容~"
        txtView.plaAlignment = .left
        txtView.font = .systemFont(ofSize: 17)
        return txtView
    }()
    
    private lazy var commitBtn: UIButton = {
        let btn = UIButton().extBgColor(color: UIColor(kshex: "#4972F9"))
            .extFont(font: .systemFont(ofSize: 18))
            .extTitleColor(color: .white)
            .extRadius(radius: 5).extMaskBounds(mask: true)
        btn.addTarget(self, action: #selector(commitBtnClick), for: .touchUpInside)
        
        return btn
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func initUI() {
        extBgColor(color: .white).extClipsToBounds(bounds: true)
        
        addSubview(commitBtn)
        commitBtn.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(30)
            make.right.equalToSuperview().offset(-30)
            make.height.equalTo(44)
            let safeArea = UIApplication.shared.windows.first?.safeAreaInsets.bottom ?? 0.0
            make.bottom.equalToSuperview().offset(-(safeArea + 10))
        }
        
        addSubview(inputTextView)
        inputTextView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.bottom.equalTo(commitBtn.snp.top).offset(-20)
            make.height.equalTo(80)
        }
    }
    
    @objc private func commitBtnClick() {
        commitBlock?(type, inputTextView.text)
    }
}
